﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WpfApp1
{
    public class Class1
    {
        public void Send()
        {
            var a = new StackFrame().GetMethod().Name;
            Console.WriteLine(a);
            Messenger.Default.Send("test", "test");
        }

        public void Write()
        {
            Thread.Sleep(2000);
            Send();
        }
    }

    public class Class2
    {
        bool InTask = false;
        string token = "";
        Class1 c1 = new Class1();
        public Class2()
        {
            Messenger.Default.Register<string>(this, "test", new Action<string>((s) => Do(s)));
        }

        void Do(string s)
        { 
            var methods = this.GetType().GetMethods();
            foreach (var m in methods)
            {
                if (token == m.Name)
                {
                  var mm=  m.GetCustomAttributes(typeof(DescriptionAttribute),false).ToList();
                    if (mm.Count > 0)
                    {
                        var aaaa = mm[0] as DescriptionAttribute;
                        var desc = aaaa.Description;
                        Console.WriteLine(desc);
                    }
                }
            }
            Thread.Sleep(100);
            InTask = false;

        }

        public void Write(string s,string token)
        {
            while (InTask)
            {
                Thread.Sleep(100);
            }
            this.token = token;
            Console.WriteLine("send a message:{0}",s);
            InTask = true;
            c1.Write();

        }

        [Description("前扫码完成")]
        public void Test(string t)
        {
            var methodName = new StackFrame().GetMethod().Name;
            Write(t, methodName);
        }
    }
}
